package com.ricchieri.manage.bean;

import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.lazymodel.ReservationLazyDataModel;
import com.ricchieri.model.Reservation;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IReservationService;
import com.ricchieri.service.interf.IReservationTypeService;
import com.ricchieri.service.interf.ITripService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "searchReservationMB")
@ViewScoped
public class SearchReservationManageBean extends GenericManageBean {

	private static final long serialVersionUID = -3624916374734996164L;

	@ManagedProperty(value = "#{ReservationService}")
	IReservationService reservationService;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	IDocumentTypeService documentTypeService;
	
	@ManagedProperty(value = "#{ReservationTypeService}")
	IReservationTypeService reservationTypeService;
	
	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;

	private ReservationFilter reservationFilter;
	
	private Map<String, String> reservationType;
	private Map<String, String> documentType;
	private Map<String, String> trips;
	private Map<String, String> reservationStatus;
	private ReservationLazyDataModel reservationLazyDataModel;
	
	@PostConstruct
	public void init() {
		reservationFilter = (ReservationFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(reservationFilter == null) {
			reservationFilter =  new ReservationFilter();
			reservationFilter.setStart(new Date (System.currentTimeMillis()));
		}
	}
	
	public IReservationService getReservationService() {
		return reservationService;
	}
	
	public void setReservationService(IReservationService reservationService) {
		this.reservationService = reservationService;
	}
	
	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}
	
	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	public ReservationFilter getReservationFilter() {
		return reservationFilter;
	}
	
	public void setReservationFilter(ReservationFilter reservationFilter) {
		this.reservationFilter = reservationFilter;
	}
	
	public Map<String, String> getReservationType() {
		if(reservationType == null) {
			reservationType = getReservationTypeService().getComboReservationType();
		}
		return reservationType;
	}
	
	public void setReservationType(Map<String, String> reservationType) {
		this.reservationType = reservationType;
	}
	
	public Map<String, String> getDocumentType() {
		if(documentType == null) {
			documentType = getDocumentTypeService().getComboDocuments();
		}
		return documentType;
	}
	
	public void setDocumentType(Map<String, String> documentType) {
		this.documentType = documentType;
	}

	public IReservationTypeService getReservationTypeService() {
		return reservationTypeService;
	}

	public void setReservationTypeService(
			IReservationTypeService reservationTypeService) {
		this.reservationTypeService = reservationTypeService;
	}

	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public Map<String, String> getTrips() {
		if(trips == null) {
			trips =  getTripService().getComboTrips();
		}
		return trips;
	}

	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}
	
	public void search() {
		reservationLazyDataModel = null;
	}
	
	public void reset() {
		reservationLazyDataModel = null;
		reservationFilter = new ReservationFilter();
	}

	public ReservationLazyDataModel getReservationLazyDataModel() {
		if(reservationLazyDataModel == null) {
			reservationFilter.setSortField("sortTime");
			reservationFilter.setSortOrder(BusinessSettings.SORT_ASCENDING);
			reservationLazyDataModel = new ReservationLazyDataModel(reservationService, reservationFilter);			
		}
		return reservationLazyDataModel;
	}

	public void setReservationLazyDataModel(
			ReservationLazyDataModel reservationLazyDataModel) {
		this.reservationLazyDataModel = reservationLazyDataModel;
	}
	
	public String viewReservation(Reservation reservation) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, getReservationFilter());
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String editReservation(Reservation reservation) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, getReservationFilter());
		return BusinessSettings.EDIT;
	}
	
	public String cancelReservation(Reservation reservation) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, reservation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, getReservationFilter());
		return BusinessSettings.CANCEL_RESERVATION;
	}

	public Map<String, String> getReservationStatus() {
		if(reservationStatus == null) {
			reservationStatus = getReservationService().getReservationStateCombo();
		}
		return reservationStatus;
	}

	public void setReservationStatus(Map<String, String> reservationStatus) {
		this.reservationStatus = reservationStatus;
	}
	
	public String getStatusDescription(Boolean status) {
		if(status) {
			return FacesUtil.getMessage("reservationCanceled");
		} else {
			return FacesUtil.getMessage("reservationActive");
		}
	}
	
}

